/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lab2;

import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Кирилл
 */
public class WarningSystem extends Thread {

    private Calendar nextTime;
    private Manager manager;

    public WarningSystem() {
        manager = Manager.getInstance();
    }

    private boolean isDateEquals(Calendar c1, Calendar c2) {
        if (c1.compareTo(c2) > 0) {
            return false;
        }
        return true;
    }

    public void run() {
        Calendar currentTime;
        while (true) {
            try {
                currentTime = Calendar.getInstance();
                Thread.sleep(10000);
                while (true) {
                    try {
                        nextTime = manager.getNextTime();
                        System.out.println(manager.getNextTime());
                        Thread.sleep(10000);
                        break;
                    } catch (InterruptedException ex) {
                        Logger.getLogger(WarningSystem.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IndexOutOfBoundsException ex) {
                    }
                }
                if (isDateEquals(nextTime, currentTime)) {
                    MainWindow.showAlert(nextTime);
                    System.out.println("111");
                }
                else {
                    System.out.println("222");
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(WarningSystem.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
